package org.example.review.feign;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Feign使用示例Controller（纯OpenFeign）
 * 演示如何在Controller中使用Feign Client
 */
@RestController
@RequestMapping("/api/feign-demo")
@RequiredArgsConstructor
public class FeignDemoController {

    // 通过构造函数注入Feign Client（由FeignClientFactory创建）
    private final ApplyServiceClient applyServiceClient;

    /**
     * 示例1：调用远程服务获取申请信息
     */
    @GetMapping("/apply/{id}")
    public Map<String, Object> getApply(@PathVariable Long id) {
        try {
            // 像调用本地方法一样调用远程服务
            Map<String, Object> result = applyServiceClient.getApplyById(id);
            return Map.of(
                    "success", true,
                    "data", result
            );
        } catch (Exception e) {
            return Map.of(
                    "success", false,
                    "message", "调用Apply服务失败: " + e.getMessage()
            );
        }
    }

    /**
     * 示例2：创建申请
     */
    @PostMapping("/apply")
    public Map<String, Object> createApply(@RequestBody Map<String, Object> applyData) {
        try {
            Map<String, Object> result = applyServiceClient.createApply(applyData);
            return Map.of(
                    "success", true,
                    "data", result
            );
        } catch (Exception e) {
            return Map.of(
                    "success", false,
                    "message", "创建申请失败: " + e.getMessage()
            );
        }
    }

    /**
     * 示例3：查询申请列表（使用QueryMap方式）
     */
    @GetMapping("/apply/list")
    public Map<String, Object> getApplyList(
            @RequestParam Long userId,
            @RequestParam(required = false) String status) {
        try {
            // 构建查询参数Map
            Map<String, Object> queryParams = new HashMap<>();
            queryParams.put("userId", userId);
            if (status != null && !status.isEmpty()) {
                queryParams.put("status", status);
            }
            
            Map<String, Object> result = applyServiceClient.getApplyList(queryParams);
            return Map.of(
                    "success", true,
                    "data", result
            );
        } catch (Exception e) {
            return Map.of(
                    "success", false,
                    "message", "查询申请列表失败: " + e.getMessage()
            );
        }
    }

    /**
     * 示例4：更新申请状态
     */
    @PutMapping("/apply/{id}/status")
    public Map<String, Object> updateStatus(
            @PathVariable Long id,
            @RequestParam String status) {
        try {
            Map<String, Object> result = applyServiceClient.updateApplyStatus(id, status);
            return Map.of(
                    "success", true,
                    "data", result
            );
        } catch (Exception e) {
            return Map.of(
                    "success", false,
                    "message", "更新状态失败: " + e.getMessage()
            );
        }
    }

    /**
     * 示例5：删除申请
     */
    @DeleteMapping("/apply/{id}")
    public Map<String, Object> deleteApply(@PathVariable Long id) {
        try {
            Map<String, Object> result = applyServiceClient.deleteApply(id);
            return Map.of(
                    "success", true,
                    "data", result
            );
        } catch (Exception e) {
            return Map.of(
                    "success", false,
                    "message", "删除申请失败: " + e.getMessage()
            );
        }
    }
}
